Ext.define('Seq.qunee.Container', {
    extend: 'Ext.Component',
    xtype: 'quneecontainer',

    option: {
        interactionMode: Q.Consts.INTERACTION_MODE_DEFAULT,
        enableRectangleSelectionByRightButton : true
    },

    setOption(option) {
        this.graph.setOption(option);
    },

    search(name) {
        let me = this, results = me.searchResults || {}, result;
        name = name.trim().toLowerCase();
        if (!Ext.isEmpty(name)) {
            if (results.name == name) {
                results.index++;
                if (results.index >= results.datas.length) {
                    results.index = 0;
                }
                result = results.datas[results.index];
            } else {
                results.datas = [];
                me.graph.graphModel.forEach((e) => {
                    if (-1 < e.name.toLowerCase().search(name)) {
                        results.datas.push(e);
                    }
                });
                results.index = -1;
                results.name = name;
                if (results.datas.length > 0) {
                    results.index = 0;
                    result = results.datas[0];
                }
            }
            me.searchResults = results;
        }
        return result;
    },

    select(item) {
        let graph = this.graph;
        if (item) {
            graph.setSelection(item);
            let bounds = graph.getUIBounds(item);
            if (bounds) {
                graph.centerTo(bounds.cx, bounds.cy, Math.max(2, graph.scale), true);
            }
        }
    },

    initGraph(view) {
        // TODO 子类用于初始化Graph
    },

    addCustomInteractions(graph) {
        graph.addCustomInteraction({
            onclick: function (evt, graph) {
                let ownerCt = graph.ownerCt;
                ownerCt.fireEvent('click', ownerCt, evt, evt.getData());
            },
            ondblclick: function (evt, graph) {
                let ownerCt = graph.ownerCt;
                ownerCt.fireEvent('dblclick', ownerCt, evt, evt.getData());
            },
            onmousemove: function (evt, graph) {
                let data = evt.getData();
                graph.cursor = data && graph.isSelectable(data) ? 'pointer' : null;
            }
        });
    },

    addInteractionDispatcher(graph) {
        graph.interactionDispatcher.addListener(function (evt) {
            let ownerCt = evt.source.ownerCt;
            switch (evt.kind) {
                case Q.InteractionEvent.SELECT:
                case Q.InteractionEvent.SELECT_END:
                    ownerCt.fireEvent('selectchange', ownerCt, evt, evt.data.datas);
            }
        });
    },

    listeners: {
        afterrender(view) {
            if (null == view.graph) {
                let graph = view.initGraph(view);
                graph.ownerCt = view;
                view.addCustomInteractions(graph);
                view.addInteractionDispatcher(graph);
                view.graph = graph;
            }
        },

        resize(view, width, height, oldWidth, oldHeight, eOpts) {
            view.graph.updateViewport();
        }
    }

});